package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.hairline.bean.Comment;
import com.hairline.bean.CommentExample;
import com.hairline.bean.vo.CommentListVO;
import com.hairline.bean.vo.CommentVO;
import com.hairline.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl  implements CommentService{

    @Autowired
    CommentMapper commentMapper;

    @Override
    public long countComments(CommentExample example) {

        return commentMapper.countByExample(example);
    }

    @Override
    public List<Comment> selectCommentsbyExample(CommentExample commentExample) {
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public boolean deleteById(Comment comment) {
        int i = commentMapper.deleteByPrimaryKey(comment.getId());
        if (i == 1) {
            return true;
        }
        return false;
    }

    public CommentListVO selectCommentsVO(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {

        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        long total = commentMapper.countByExample(commentExample);

        if (userId != null && !userId.equals("")) {
            criteria.andUserIdEqualTo( userId );
        }
        if (valueId != null && !valueId.equals("")) {
            criteria.andValueIdEqualTo(valueId);
        }
        PageHelper.startPage(page,limit);
        commentExample.setOrderByClause(sort + " " + order );
        List<CommentVO> commentVOList = commentMapper.selectAllComment(commentExample);
        CommentListVO commentListVO = new CommentListVO(commentVOList, total);
        return commentListVO;
    }

}
